//+------------------------------------------------------------------+
//|                                                    Entry_Bar.mq4 |
//| Trading Manual on Entry_Bars                                     |
//|                                                                  |
//+------------------------------------------------------------------+
#property  copyright "by Sui Yau, 2012"
#property  link      "http://www.dealitup.com"


//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 7
#property  indicator_color1  SlateBlue
#property  indicator_color2  Red
#property  indicator_color3  SlateBlue
#property  indicator_color4  Crimson
#property  indicator_width4   3
#property  indicator_color5  SteelBlue
#property  indicator_width5   3
#property  indicator_color6  Crimson
#property  indicator_width6   3
#property  indicator_color7  SteelBlue
#property  indicator_width7   3


//---- indicator parameters
extern int BobaPeriod= 20;
extern int BobaDeviations= 2;


//---- indicator buffers
double     BufferBand1[];
double     BufferBand0[];
double     BufferBand2[];
double     BufferSell[];
double     BufferBuy[];
double     BufferSellEntry[];
double     BufferBuyEntry[];



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
{
   //---- 2 additional buffers are used for counting.
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
   
   // two bollies
   SetIndexBuffer(0,BufferBand1);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexDrawBegin(0,BobaPeriod);

   SetIndexBuffer(1,BufferBand0);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexDrawBegin(1,BobaPeriod);
   
   SetIndexBuffer(2,BufferBand2);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexDrawBegin(2,BobaPeriod);
   
   // marks for gimmees
   SetIndexBuffer(3,BufferSell);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexDrawBegin(3,BobaPeriod);
   SetIndexArrow(3, 167);
   SetIndexEmptyValue(3, 0);

   SetIndexBuffer(4,BufferBuy);  
   SetIndexStyle(4,DRAW_ARROW);
   SetIndexDrawBegin(4,BobaPeriod);
   SetIndexArrow(4, 167);
   SetIndexEmptyValue(4, 0);
   
   // marks for entries
   SetIndexBuffer(5,BufferSellEntry);
   SetIndexStyle(5,DRAW_ARROW);
   SetIndexDrawBegin(5,BobaPeriod);
   SetIndexArrow(5, 238);
   SetIndexEmptyValue(5, 0);

   SetIndexBuffer(6,BufferBuyEntry);  
   SetIndexStyle(6,DRAW_ARROW);
   SetIndexDrawBegin(6,BobaPeriod);
   SetIndexArrow(6, 236);
   SetIndexEmptyValue(6, 0);

   
    
   //---- name for DataWindow and indicator subwindow label
   // IndicatorShortName("GimmeBar ");
   
   //---- initialization done
   return(0);
}



//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int counted_bars= IndicatorCounted(),
         lastbar;
     
   if (counted_bars>0)
      counted_bars--;
      
   lastbar= Bars - counted_bars;
   
   GimmeeBar(0, lastbar, BufferSell, BufferBuy, BufferSellEntry, BufferBuyEntry, BufferBand1, BufferBand0, BufferBand2, BobaPeriod, BobaDeviations);

   return (0);
}   




//+------------------------------------------------------------------+
//| Mark Gimmee-Bars and mark possible entries for deals              |
//+------------------------------------------------------------------+
double GimmeeBar(int offset, int lastbar, double &sellbuf[], double &buybuf[], double &sellbuf2[], double &buybuf2[], 
                  double &band1buf[], double &band0buf[], double &band2buf[], int period, int deviation)
{
   double band1, band0, band2;
   int markerdist= 5;   // distance between bars and marker dots
   
   lastbar= MathMin(Bars-period, lastbar);   

   //---- main loop
   for(int i= lastbar; i>=offset; i--){
   
      sellbuf[i]= 0;
      buybuf[i]=0;
      sellbuf2[i]= 0;
      buybuf2[i]=0;

      band0=iBands(NULL,0, period, deviation, 0,PRICE_CLOSE,MODE_MAIN,i);
      band1=iBands(NULL,0, period, deviation, 0,PRICE_CLOSE,MODE_UPPER,i); 
      band2=iBands(NULL,0, period, deviation, 0,PRICE_CLOSE,MODE_LOWER,i);

      band1buf[i]= band1;
      band0buf[i]= band0;
      band2buf[i]= band2;
      
     
      // 1. Prices were rising.
      // 2. Prices touched the upper band.
      // 3. The price bar closed lower than it
      // opened when prices were previously rising.
      // or vice versa
      
      if (High[i]>band1 && Low[i]<band1 && Low[i]>band0 && Open[i+1]<Close[i+1] && Open[i]>Close[i]) 
      {
         sellbuf[i]= High[i] + markerdist*Point;
      }
      else
      if (Low[i]<band2 && High[i]>band2 && High[i]<band0 && Open[i+1]>Close[i+1] && Open[i]<Close[i]) 
      {
         buybuf[i]= Low[i] - markerdist*Point;
      }
 
      // Should such a price bar occur, a sell short order is to be executed
      // one tick below the low of the Gimmee bar.

      if (sellbuf[i+1]!=0 && Low[i]>band0 && Low[i]<Low[i+1]-1*Point) 
      {
         sellbuf2[i]= Low[i+1]-1*Point;
      }
      
      if (buybuf[i+1]!=0 && High[i]<band0 && High[i]>High[i+1]+1*Point) 
      {
         buybuf2[i]= High[i+1]+1*Point;
      }
      
   }

   /*
   if (DebugLogger)
      Print(TimeOffset(offset), "BG-GimmeBar ", "");
   */
   
   return (0); 
}

//+------------------------------------------------------------------+

